using Aurora.AI.Common.Models;
using Aurora.AI.Gateway.Models.Setting;
using Aurora.AI.Gateway.Services;
using Aurora.AI.Wiki.Services;

namespace Gateway.UI.Api.Controllers.Ai;

/// <summary>
/// ai setting 控制器
/// </summary>
[ApiController]
[Route("api/ai/setting")]
public class AiSettingController(SettingService settingService,
    WikiSettingService wikiSettingService) : BaseController
{
    /// <summary>
    /// 设置
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost, Route("set")]
    public async Task<bool> Set(AISettingRequest input)
    {
        // json 格式化
        await settingService.Update(input);
        if (input.Common != null)
        {
            await wikiSettingService.Update(input.Common);
        }

        return true;
    }

    /// <summary>
    /// 获取配置详情
    /// </summary>
    /// <returns></returns>
    [HttpGet, Route($"detail")]
    public async Task<AISettingRequest> GetDetail()
    {
        // 去查询 数据
        var aiSetting = await settingService.GetJson();
        // 查询 ai common 
        AICommonSetting aiCommonSetting = await wikiSettingService.GetCommonSetting();
        if (aiCommonSetting == null)
        {
            aiCommonSetting = new AICommonSetting();
        }

        aiSetting.Common = aiCommonSetting;
        return aiSetting;
    }
}